//
//  XLTabBar.m
//  XLTabBarController
//
//  Created by ZxlAndZyp on 2019/1/8.
//  Copyright © 2019 zuoxueliang. All rights reserved.
//

#import "XLTabBar.h"
#import "XLUtils/UIView+XLExtension.h"
#import "XLTabBarButton.h"

@interface XLTabBar ()

@property (nonatomic, strong) NSMutableArray *tabBarArr; /**< 存放tabBarButton数组 */

@property (nonatomic, strong) NSMutableArray *norImages; /**< 普通图片数组 */

@property (nonatomic, strong) NSMutableArray *selImages; /**< 高亮图片数组 */

@property (nonatomic, strong) NSMutableArray *titles; /**< 标题数组 */
@end

@implementation XLTabBar

- (instancetype)initWithFrame:(CGRect)frame norImages:(NSArray *)norImages selImages:(NSArray *)selImages titles:(NSArray *)titles config:(XLTabBarConfig *)config
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = config.tabBarBgColor;
        [self topLineIsClearColor:config.hiddenTopLine];
        
        for (int i = 0; i < norImages.count; i++) {
            XLTabBarButton *tabBarBtn = [[XLTabBarButton alloc] init];
            tabBarBtn.imageView.image = [UIImage imageNamed:norImages[i]];
            tabBarBtn.titleL.text = titles[i];
            tabBarBtn.titleL.textColor = config.norTitleColor;
            tabBarBtn.layoutType = config.layoutType;
            tabBarBtn.tag = i;
            [self addSubview:tabBarBtn];
        
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [tabBarBtn addGestureRecognizer:tap];
            [self.tabBarArr addObject:tabBarBtn];
            [self.titles addObjectsFromArray:titles];
            [self.norImages addObjectsFromArray:norImages];
            [self.selImages addObjectsFromArray:selImages];
        }
    }
    return self;
}

- (void)tapClick:(UITapGestureRecognizer *)tap {

    [self setUpSelectedIndex:tap.view.tag] ;

    if ([self.tabBarDelegate respondsToSelector:@selector(tabBar:disSelectIndex:)]) {
        [self.tabBarDelegate tabBar:self disSelectIndex:tap.view.tag];
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    [self setUpSelectedIndex:selectedIndex];
}

- (void)setUpSelectedIndex:(NSInteger)selectedIndex
{
    XLTabBarConfig *config = [XLTabBarConfig tabBarConfig];
    for (int i = 0; i < self.tabBarArr.count; i++) {
        XLTabBarButton *tabBarBtn = self.tabBarArr[i];
        if (i == selectedIndex) {
            tabBarBtn.titleL.textColor = config.selTitleColor;
            tabBarBtn.imageView.image = [UIImage imageNamed:self.selImages[i]];
        }else{
            tabBarBtn.titleL.textColor = config.norTitleColor;
            tabBarBtn.imageView.image = [UIImage imageNamed:self.norImages[i]];
        }
    }
}

#pragma mark-
#pragma mark 顶部线条处理(清除颜色)
- (void)topLineIsClearColor:(BOOL)isClearColor {
    UIColor *color = [UIColor clearColor];
    if (!isClearColor) {
        color = [[XLTabBarConfig tabBarConfig] topLineColor];
    }
    
    CGRect rect = CGRectMake(0, 0, self.zxl_width, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self setBackgroundImage:[UIImage new]];
    [self setShadowImage:img];
}

#pragma mark-
#pragma mark 布局

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSMutableArray *tempArr = [NSMutableArray arrayWithCapacity:0];
    for (UIView *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
        
        if ([tabBarButton isKindOfClass:[XLTabBarButton class]] ||
            [tabBarButton isKindOfClass:[UIButton class]]) {
            [tempArr addObject:tabBarButton];
        }
    }
    for (int i = 0; i < tempArr.count; i++) {
        UIView *tabBarBtn = tempArr[i];
        if ([tabBarBtn isKindOfClass:[UIButton class]]) {
            [tempArr insertObject:tabBarBtn atIndex:tabBarBtn.tag];
            [tempArr removeLastObject];
            break;
        }
    }
    
    CGFloat tabBarBtnW = self.zxl_width / tempArr.count;
    CGFloat tabBarBtnH = 49;
    CGFloat tabBarBtnY = 0;
    
    for (int i = 0; i < tempArr.count; i++) {
        CGFloat tabBarBtnX = i *tabBarBtnW;
        UIView *tabBarBtn = tempArr[i];
        tabBarBtn.frame = CGRectMake(tabBarBtnX, tabBarBtnY, tabBarBtnW, tabBarBtnH);
    }
}


#pragma mark-
#pragma mark set&&get

- (NSMutableArray *)tabBarArr
{
    if (!_tabBarArr) {
        _tabBarArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _tabBarArr;
}

- (NSMutableArray *)norImages
{
    if (!_norImages) {
        _norImages = [NSMutableArray arrayWithCapacity:0];
    }
    return _norImages;
}

- (NSMutableArray *)selImages
{
    if (!_selImages) {
        _selImages = [NSMutableArray arrayWithCapacity:0];
    }
    return _selImages;
}

- (NSMutableArray *)titles
{
    if (!_titles) {
        _titles = [NSMutableArray arrayWithCapacity:0];
    }
    return _titles;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
